<?php
/**
 * The RssWriter class
 *
 */
class RssWriter
{
	private $version = 'rss20';

	private $items = '';

	public $rss = '';

	public function rss($array)
	{
		foreach($array['body'] as $value)
		{
			$this->addItem($value);
		}
		$this->releaseRSS($array['head']) ;
	}

	public function rssVer($version)
	{
		$this->version = $version;
	}

	private function strip($s)
	{
		$s = str_replace('&', '&amp;', $s);
		$s = str_replace('<', '&lt;', $s);
		$s = str_replace('>', '&gt;', $s);
		if ('' == $s)
		{
			$s = ' ';
		}
		return $s;
	}

	private function addItem($a)
	{
		switch ($this->version)
		{
			case 'rss092':
				$item = "\t" . '<item>' . "\n";
				$item .= "\t\t" . '<title>' . $this->strip($a['topic']) . '</title>				' . "\n";
				$item .= "\t\t" . '<description><![CDATA[' . $a['abstract'] . ']]></description>' . "\n";
				$item .= "\t\t" . '<link>' . $this->strip($a['link']) . '</link>' . "\n";
				$item .= "\t" . '</item>' . "\n";
				break;
			case 'rss10':
				$item = "\t" . '<item rdf:about="' . $this->strip($a['link']) . '">' . "\n";
				$item .= "\t\t" . '<title>' . $this->strip($a['topic']) . '</title>' . "\n";
				$item .= "\t\t" . '<dc:title>' . $this->strip($a['topic']) . '</dc:title>' . "\n";
				$item .= "\t\t" . '<description><![CDATA[' . $a['abstract'] . ']]></description>' . "\n";
				$item .= "\t\t" . '<link>' . $this->strip($a['link']) . '</link>' . "\n";
				$item .= "\t\t" . '<dc:date>' . substr($a['date'], 0, 4) . '-' . substr($a['date'], 4, 2) . '-' . substr($a['date'], 6, 2) . ' ' . substr($a['date'], 8, 2) . ':' . substr($a['date'], 10, 2) . ':' . substr($a['date'], 12, 4) . '</dc:date>' . "\n";
				$item .= "\t\t" . '<dc:creator>' . $this->strip($a['author']) . '</dc:creator>' . "\n";
				$item .= "\t" . '</item>' . "\n";
				break;
			case 'rss20':
				$item = "\t" . '<item>' . "\n";
				$item .= "\t\t" . '<title>' . $this->strip($a['topic']) . '</title>' . "\n";
				$item .= "\t\t" . '<link>' . $this->strip($a['link']) . '</link>' . "\n";
				$item .= "\t\t" . '<pubDate>' . date('D, d M Y H:i:s', $a['date']) . ' GMT</pubDate>' . "\n";
				$item .= "\t\t" . '<description><![CDATA[' . $a['abstract'] . ']]></description>' . "\n";
				$item .= "\t\t" . '<author>' . $this->strip($a['author']) . '</author>' . "\n";
				$item .= "\t" . '</item>' . "\n";
				break;
			case 'atom':
				$item = "\t" . '<entry>' . "\n";
				$item .= "\t\t" . '<title>' . $this->strip($a['topic']) . '</title>' . "\n";
				$item .= "\t\t" . '<link>' . $this->strip($a['link']) . '</link>' . "\n";
				$item .= "\t\t" . '<created>' . substr($a['date'], 0, 4) . '-' . substr($a['date'], 4, 2) . '-' . substr($a['date'], 6, 2) . 'T' . substr($a['date'], 8, 2) . ':' . substr($a['date'], 10, 2) . ':' . substr($a['date'], 12, 4) . '</created>' . "\n";
				$item .= "\t\t" . '<summary><![CDATA[' . $a['abstract'] . ']]></summary>' . "\n";
				$item .= "\t\t" . '<author>' . "\n";
				$item .= "\t\t\t" . '<name>' . $this->strip($a['author']) . '</name>' . "\n";
				$item .= "\t\t" . '</author>' . "\n";
				$item .= "\t\t" . '<dc:subject>' . $this->strip($a['topic']) . '</dc:subject>' . "\n";
				$item .= "\t\t" . '<content><![CDATA[' . $a['detail'] . ']]></content>' . "\n";
				$item .= "\t" . '</entry>' . "\n";
				break;
		}
		$this->items .= $item;
	}

	private function releaseRSS($fa)
	{
		$this->rss = '<?xml version="1.0" encoding="utf-8"?>' . "\n";
		// $fa = func_get_args();
		switch ($this->version)
		{
			case 'rss092':
				$this->rss .= '<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns="http://my.netscape.com/rdf/simple/0.9/">' . "\n";
				$this->rss .= '<channel>' . "\n";
				$this->rss .= "\t" . '<title>' . $this->strip($fa[0]) . '</title>' . "\n";
				$this->rss .= "\t" . '<description><![CDATA[' . $fa[1] . ']]></description>' . "\n";
				$this->rss .= "\t" . '<link>' . $this->strip($fa[2]) . '</link>' . "\n";
				$this->rss .= '</channel>' . "\n";
				$this->rss .= $this->items;
				$this->rss .= '</rdf:RDF>';
				break;
			case 'rss10':
				$this->rss .= '<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns="http://purl.org/rss/1.0/" xmlns:dc="http://purl.org/dc/elements/1.1/">' . "\n";
				$this->rss .= '<channel>' . "\n";
				$this->rss .= "\t" . '<title>' . $this->strip($fa[0]) . '</title>' . "\n";
				$this->rss .= "\t" . '<description><![CDATA[' . $fa[1] . ']]></description>' . "\n";
				$this->rss .= "\t" . '<link>' . $this->strip($fa[2]) . '</link>' . "\n";
				$this->rss .= $this->items;
				$this->rss .= '</channel>' . "\n";
				$this->rss .= '</rdf:RDF>' . "\n";
				break;
			case 'rss20':
				$this->rss .= '<rss version="2.0">' . "\n";
				$this->rss .= '<channel>' . "\n";
				$this->rss .= "\t" . '<title>' . $this->strip($fa[0]) . '</title>' . "\n";
				$this->rss .= "\t" . '<description><![CDATA[' . $fa[1] . ']]></description>' . "\n";
				$this->rss .= "\t" . '<link>' . $this->strip($fa[2]) . '</link>' . "\n";
				$this->rss .= "\t" . '<lastBuildDate>' . date('D, d M Y H:i:s', $fa[3]) . ' GMT</lastBuildDate>' . "\n";
				$this->rss .= "\t" . '<language>utf-8</language>' . "\n";
				$this->rss .= "\t" . '<copyright>sulwan@126.com</copyright>' . "\n";
				$this->rss .= $this->items;
				$this->rss .= '</channel>' . "\n";
				$this->rss .= '</rss>' . "\n";
				break;
			case 'atom':
				$this->rss .= '<feed version="0.3" xmlns="http://purl.org/atom/ns#" xmlns:dc="http://purl.org/dc/elements/1.1/" xml:lang="zh-cn">' . "\n";
				$this->rss .= "\t" . '<title>' . $this->strip($fa[0]) . '</title>' . "\n";
				$this->rss .= "\t" . '<link>' . $this->strip($fa[2 ]) . '</link>' . "\n";
				$this->rss .= "\t" . '<modified>' . substr($fa[3], 0, 4) . '-' . substr($fa[3], 4, 2) . '-' . substr($fa[3], 6, 2) . 'T' . substr($fa[3], 8, 2) . ':' . substr($fa[3], 10, 2) . ':' . substr($fa[3], 12, 4) . '</modified>' . "\n";
				$this->rss .= "\t" . '<tagline><![CDATA[' . $fa[1] . ']]></tagline>' . "\n";
				$this->rss .= "\t" . '<generator>Blogchin RSSFeed Generator</generator>' . "\n";
				$this->rss .= "\t" . '<copyright>Copyright (C); 2004, BlogChina.com</copyright>' . "\n";
				$this->rss .= $this->items;
				$this->rss .= '</feed>' . "\n";
				break;
		}

	}
}
